// OriginalPropertySet.cpp: implementation of the OriginalPropertySet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Phsshare.h"
#include "OriginalPropertySet.h"
#include "DBConnectionManager.h"
using namespace DAWrapper;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace PropertySystem
{
	OriginalPropertySet::OriginalPropertySet()
	{
		
	}
	
	OriginalPropertySet::~OriginalPropertySet()
	{
		RemoveAll();
		Close();
	}
	
	BOOL OriginalPropertySet::GetOriginalPropertySet(const CString& strPartID,
		const CString& strPropertyTable,
		const CString& strSource)
	{
		ASSERT(!strPartID.IsEmpty());
		ASSERT(!strPropertyTable.IsEmpty());
		CString strSql;
		strSql.Format(_T("select * from %s where ID = '%s'"), strPropertyTable, strPartID);
		
		return GetOriginalPropertySet(strSql, strSource);
	}
	
	BOOL OriginalPropertySet::GetOriginalPropertySet(INT strPartID,
		const CString& strPropertyTable,
		const CString& strSource)
	{
		ASSERT(!strPropertyTable.IsEmpty());
		CString strSql;
		strSql.Format(_T("select * from %s where ID = %d"), strPropertyTable, strPartID);
		
		return GetOriginalPropertySet(strSql, strSource);
	}
	
	BOOL OriginalPropertySet::GetOriginalPropertySet(const CString& strSql, const CString& strSource)
	{
		ASSERT(!strSource.IsEmpty());
		ASSERT(!strSql.IsEmpty());
		
		_ConnectionPtr* pCon = DBConnectionManager::Instance()->GetConnectionPtrByUri(strSource);
		
		try
		{
			m_rs = (*pCon)->Execute(_bstr_t(strSql), NULL, adCmdText);
			ASSERT(m_rs != NULL);
		}
		CATCH_COM_ERROR(m_rs, FALSE);
		
		return TRUE;
	}
	
	OriginalProperty* OriginalPropertySet::GetFirst()
	{
		try
		{
			if(m_rs == NULL || (m_rs->adoEOF && m_rs->BOF))
				return NULL;
			
			m_rs->MoveFirst();
			return GetOriginalProperty();
		}
		CATCH_COM_ERROR(m_rs, NULL)
			
	}
	
	OriginalProperty* OriginalPropertySet::GetNext()
	{
		try
		{
			m_rs->MoveNext();
			if(m_rs == NULL || m_rs->adoEOF || m_rs->BOF)
			{		
				Close();
				return NULL;
			}
			
			return GetOriginalProperty();
		}
		CATCH_COM_ERROR(m_rs, NULL)
	}

	void OriginalPropertySet::RemoveAll()
	{
		for(int i = 0; i < m_theOPropertys.GetSize(); i++)
		{
			delete m_theOPropertys.GetAt(i);
		}
		m_theOPropertys.RemoveAll();
	}

	void OriginalPropertySet::Close()
	{
		if(m_rs != NULL && m_rs->GetState() == adStateOpen)
			m_rs->Close();
	}

	OriginalProperty* OriginalPropertySet::GetOriginalProperty()
	{
		OriginalProperty* pProperty = new OriginalProperty;
		ASSERT(pProperty);

		try
		{
			pProperty->Customization(vtob(m_rs->GetCollect(_T("Customization"))));
			pProperty->Save(vtob(m_rs->GetCollect(_T("Save"))));
			pProperty->DataType(vtos(m_rs->GetCollect(_T("DataType"))));
			pProperty->Default(vtos(m_rs->GetCollect(_T("Default"))));
			pProperty->Description(vtos(m_rs->GetCollect(_T("Description"))));
			pProperty->Influence(vtos(m_rs->GetCollect(_T("Influence"))));
			pProperty->InputMode(vtos(m_rs->GetCollect(_T("PromptInputMode"))));
			pProperty->PartID(vtos(m_rs->GetCollect(_T("ID"))));
			pProperty->Prompt(vtos(m_rs->GetCollect(_T("Prompt"))));
			pProperty->PromptSource(vtos(m_rs->GetCollect(_T("PromptDataSource"))));
			pProperty->PromptSourceMode(vtos(m_rs->GetCollect(_T("PromptDataSourceMode"))));
			pProperty->PropertyName(vtos(m_rs->GetCollect(_T("PropertyName"))));
			pProperty->PropertyTable(vtos(m_rs->GetCollect(_T("PropertyTableName"))));
			pProperty->PropertyType(vtos(m_rs->GetCollect(_T("PropertyStyle"))));
			pProperty->Unit(vtos(m_rs->GetCollect(_T("Unit"))));
			pProperty->EffectField(vtos(m_rs->GetCollect(_T("EffectField"))));
			pProperty->PropertyField(vtos(m_rs->GetCollect(_T("PropertyField"))));
			pProperty->EffectCondition(vtos(m_rs->GetCollect(_T("EffectCondition"))));
			pProperty->ExistCondition(vtos(m_rs->GetCollect(_T("ExistCondition"))));
			pProperty->ArrangePrior(vtoi(m_rs->GetCollect(_T("Arrange_Prior"))));
		}
		catch(_com_error e)
		{
			ASSERT(FALSE);
			ErrorAlert(e);
			delete pProperty;
			return NULL;
		}

		m_theOPropertys.Add(pProperty);

		return pProperty;
	}
};